package com.platform.user.service;

import com.platform.bean.JsonResult;
import com.platform.user.dao.ChatMessageDao;
import com.platform.user.dao.UserDao;
import com.platform.user.dto.ChatMessageInfoDto;
import com.platform.user.entity.ChatMessage;
import com.platform.user.entity.ChatUser;
import com.platform.user.entity.User;
import com.platform.utils.Base64Utils;
import com.platform.utils.PrintMessageUtils;
import com.platform.utils.UploadFileToRemoteUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;

@Service
public class ChatMessageService {

    @Resource
    private ChatMessageDao chatMessageDao;
    @Resource
    private UserDao userDao;

    public List<ChatMessageInfoDto> queryMessageByPage(Map<String, Object> params) {
        List<ChatMessage> chatMessageList = chatMessageDao.queryMessageByPage(params);

        List<ChatMessageInfoDto> chatMessageInfoDtoList = new ArrayList<>();
        // List<User> userList = new ArrayList<>();
        Map<Integer, User> userMap = new HashMap<>();
        for(ChatMessage x: chatMessageList) {
            ChatMessageInfoDto chatMessageInfoDto = new ChatMessageInfoDto();
            chatMessageInfoDto.setChatMessage(x);
            chatMessageInfoDto.setIfOnlineMessage(0);
            if (userMap.get(x.getUserId()) == null) {
                User user = userDao.queryById(x.getUserId());
                user.setFullName(null);
                user.setEmail(null);
                user.setPassword(null);
                chatMessageInfoDto.setUser(user);
                userMap.put(user.getId(), user);
            } else {
                chatMessageInfoDto.setUser(userMap.get(x.getUserId()));
            }
            chatMessageInfoDtoList.add(chatMessageInfoDto);
        }
        Collections.reverse(chatMessageInfoDtoList);
        return chatMessageInfoDtoList;
    }

    public String uploadMessageImage(Map<String, Object> params) throws IOException {
        String messageImageBase64 = (String) params.get("messageImageBase64");

        if (!StringUtils.isBlank(messageImageBase64)) {
            return saveProfilePictureToUrl(messageImageBase64);
        }
        return null;
    }

    /**
     * 将base64封面图片转换为url
     *
     * @param profilePicture base64图片
     * @return 图片url
     * @throws IOException 异常
     */
    private String saveProfilePictureToUrl(String profilePicture) throws IOException {
        StringBuilder fileName = new StringBuilder();
        Map<String, String> imageTypeAndCode = Base64Utils.splitImageTypeAndCode(profilePicture);
        String imageType = null;
        String base64Data = null;

        if (imageTypeAndCode != null) {
            imageType = imageTypeAndCode.get("imageType");
            base64Data = imageTypeAndCode.get("base64Data");
        }

        fileName.append(System.currentTimeMillis()).append(".").append(imageType);
        // Base64转文件
        MultipartFile multipartFile = Base64Utils.convertBase64ToMultipart(base64Data, fileName.toString());
        JsonResult jsonResult = UploadFileToRemoteUtils.uploadFileToRemoteServer(multipartFile, "/opt/docker/apache/www/images/lost_and_found");
        return (String) jsonResult.getData();
    }
}
